﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CSharpConsoleTestSolution.Demo {
    public class P27_override_polymorphism {

        /**
         * ecma 标准文档, 时间: 17:30左右
         * https://ecma-international.org/publications-and-standards/standards/ecma-334/
         */
        public static void test() {
            Console.WriteLine("0.在指定的范围内产生一系列整型数");
            Console.WriteLine("IEnumerable<int> ranges = Enumerable.Range(start: 0, count: 10);");
            Console.Write("ranges: ");
            IEnumerable<int> ranges = Enumerable.Range(start: 0, count: 10);
            foreach (var range in ranges) {
                Console.Write($"{range} ");
            }
            Console.WriteLine();
        }

        /**
         * 1.重写 & 多态, 时间: 22:20
         */
        public static void virtual_override_test() {
            Console.WriteLine();
            Console.WriteLine("1.重写 & 多态");
            Console.WriteLine("父类方法/变量: virtual, 子类方法/变量: override");
            Console.WriteLine();
            Console.WriteLine("if重写方法/变量: 父类 virtual 不能省略, 子类 [override]");
            Console.WriteLine("if子类没有 [override], 就是子类对父类方法/变量的隐藏(实际开发中一般不会这样), 区别:");
            Console.WriteLine("Vehicle car0 = new Car { Speed = 100 };");
            Console.WriteLine("Vehicle car1 = new Car_Override { Speed = 100 };");
            Console.WriteLine("car0.Run();");
            Console.WriteLine("car1.Run();");
            Vehicle car0 = new Car { Speed = 100 };
            Vehicle car1 = new Car_Override { Speed = 100 };
            car0.Run();
            car1.Run();
        }
        
        private class Vehicle {
            public virtual int Speed{ get; set; }
            /**
             * virtual: 虚函数
             */
            public virtual void Run() { 
                Console.WriteLine($"父类: Vehicle.Speed = {Speed}");
            }
        }

        private class Car: Vehicle {
            /**
             * new: 隐藏变量, 表示子类有1个同名的变量(可不写, 但标黄)
             */
            public /*override*/ int Speed{ get; set; }
            /**
             * new: 隐藏方法, 表示子类有1个同名的方法(可不写, 但标黄)
             */
            public /*new*/ /*override*/ void Run() {
                Console.WriteLine($"子类: Car.Speed = {Speed}");
            }
        }

        private class Car_Override: Vehicle {
            /**
             * new: 隐藏变量, 表示子类有1个同名的变量(可不写, 但标黄)
             */
            public override int Speed{ get; set; }
            /**
             * new: 隐藏方法, 表示子类有1个同名的方法(可不写, 但标黄)
             */
            public override void Run() {
                Console.WriteLine($"子类: Car_Override.Speed = {Speed}");
            }
        }
    }

}